package com.xs.project.job;

import com.xs.project.cache.UserCacheMap;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.Map;
import java.util.concurrent.ConcurrentMap;

@Component
@EnableScheduling
public class CacheClearJob {

    @Scheduled(cron = "0 1/1 * * * ? ")
    public void clearCache() {
        //每10分钟清理一次缓存
        Date date = new Date();
        ConcurrentMap<String, Object> cacheMap = UserCacheMap.cacheMap;
        for (Map.Entry<String, Object> entry : cacheMap.entrySet()) {
            String key = entry.getKey();
            Date value = (Date) entry.getValue();
            if (date.getTime() - value.getTime()>= 10 * 60 * 1000){
                UserCacheMap.remove(key);
            }
        }
    }
}
